<?php

namespace app\modules\admin\services;

use app\common\services\BaseService;
use app\config\Constants;
use app\models\GoodsOrder;
use app\models\User;
use app\models\UserInfoStore;
use app\models\UserInfoUser;

class StatisService extends BaseService{

    public static function aggregate(){
        //昨日订单数、昨日订单金额、待调单、昨日增加用户数、昨日新增意向商户数,
        //待审核商户数,待审核用户数
        return [
            'yes_order_number'=>self::yesterdayOrderNumber(),
            'yes_order_price'=>self::yesterdayOrderPrice() ?? 0,
            'wait_adjust_count'=>self::waitAdjustOrderCount(),
            'yes_add_user_number'=>self::yesterdayAddUserNumber(),
            'wait_verify_store_number'=>self::waitVerifyStoreNumber(),
            'wait_verify_user_number'=>self::waitVerifyUserNumber(),
        ];
    }

    public static function yesterdayOrderNumber(){
        return GoodsOrder::find()
            ->where(["date_format(add_time,'%Y-%m-%d')"=>date("Y-m-d",strtotime("-1 day"))])
            ->andWhere("status != 2")
            ->count(1);
    }

    public static function yesterdayOrderPrice(){
        return GoodsOrder::find()
            ->where(["date_format(add_time,'%Y-%m-%d')"=>date("Y-m-d",strtotime("-1 day"))])
            ->andWhere("status != 2")
            ->sum("total_price");
    }

    public static function waitAdjustOrderCount(){
        return GoodsOrder::find()
            ->andWhere(['status'=>Constants::ORDER_ADJUST])
            ->count(1);
    }

    public static function yesterdayAddUserNumber(){
        return User::find()
            ->where(["date_format(add_time,'%Y-%m-%d')"=>date("Y-m-d",strtotime("-1 day"))])
            ->count(1);
    }

    public static function waitVerifyStoreNumber(){
        return UserInfoStore::find()->where(['is_verify'=>Constants::VERIFY_WAIT])->count(1);
    }

    public static function waitVerifyUserNumber(){
        return UserInfoUser::find()->where(['is_verify'=>Constants::VERIFY_WAIT])->count(1);
    }
}